
/**
 * AppleIIGo
 * Grappler Plus class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include "GrapplerPlus.h"

GrapplerPlus::GrapplerPlus(Motherboard * motherboard,
						   char * slotDeviceName) :
SlotDevice(motherboard, slotDeviceName) {
}

void GrapplerPlus::onMemoryRequested() {
	romMemoryOffset =
		motherboard->getMemory()->requestMemory(GRAPPLERPLUS_ROM_BYTENUM);
}

void GrapplerPlus::onMemoryAllocated() {
	romMemory = motherboard->getMemory()->getMemory(romMemoryOffset);
	char * romImage =
		"Orange Micro Grappler Plus Printer Interface ROM 3.2.bin";
	motherboard->getResources()->readResource(romMemory, 
											  GRAPPLERPLUS_ROM_BYTENUM,
											  romImage);
}

void GrapplerPlus::onResetAsserted(bool isRestart) {
	if (!isRestart)
		return;
	
	setRomBank2(false);
}

void GrapplerPlus::writeIo(UINT16 address, UINT8 value) {
	setRomBank2(address & 0x1);
}

UINT8 GrapplerPlus::readIo(UINT16 address) {
	return timingGenerator->readFloatingBus();
}

UINT8 GrapplerPlus::readMemory(UINT16 address) {
	if (address & 0x800)
		return romMemory[romBankOffset + (address & 0x7ff)];
	else {
		setRomBank2(false);
		return romMemory[address & 0xff];
	}
}

void GrapplerPlus::setRomBank2(bool value) {
	romBankOffset = value ? 0x800 : 0x000;
}
